<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="设备类型" prop="types">
        <el-select
          v-model="queryParams.types"
          placeholder="设备类型"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.charge_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="设备编号" prop="number">
        <el-autocomplete
          v-model="queryParams.number"
          :fetch-suggestions="querySearchAsync"
          placeholder="请输入设备编号"
          clearable
        ></el-autocomplete>
      </el-form-item>
      <el-form-item label="报修时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="完成报修时间">
        <el-date-picker
          v-model="dateRangeCompleted"
          style="width: 240px"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="报修状态" prop="requestStatus">
        <el-select
          v-model="queryParams.requestStatus"
          placeholder="报修状态"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="item in dict.type.request_status"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="RepairRecordList" @selection-change="handleSelectionChange">
      <el-table-column fixed label="设备编号" align="center" min-width="10%" prop="pileNumber" />
      <el-table-column label="设备类型" align="center" min-width="10%" prop="types">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.charge_type" :value="scope.row.types"/>
        </template>
      </el-table-column>
      <el-table-column label="报修时间" align="center" min-width="15%" prop="requestTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.requestTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="维修完成时间" align="center" min-width="15%" prop="completedTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.completedTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="报修人" align="center" min-width="10%" prop="nickName"></el-table-column>
      <el-table-column label="报修人电话" align="center" min-width="10%" prop="userPhone"></el-table-column>
      <el-table-column label="报修内容" align="center" min-width="20%" prop="requestContent">
        <template slot-scope="scope">
          <el-input :disabled="true" type="textarea" rows="4" v-model="scope.row.requestContent"/>
        </template>
      </el-table-column>
      <el-table-column label="报修回复" align="center" min-width="20%" prop="adminResponse">
        <template slot-scope="scope">
          <el-input :disabled="true" type="textarea" rows="4" v-model="scope.row.adminResponse"/>
        </template>
      </el-table-column>
      <el-table-column label="报修状态" align="center" min-width="10%" prop="requestStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.request_status" :value="scope.row.requestStatus"/>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center" min-width="10%" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            :disabled="scope.row.requestStatus === '2'"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改对话框 -->
    <el-dialog title="修改报修状态" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="报修状态" prop="requestStatus">
          <el-select
            :disabled="disabled"
            v-model="form.requestStatus"
            placeholder="请选择报修状态"
            clearable
            style="width: 240px"
          >
            <el-option
              v-for="dict in dict.type.request_status.filter(item => item.label !== '尚未维修')"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="报修回复" prop="adminResponse">
          <el-input :disabled="disabled" type="textarea" v-model="form.adminResponse" placeholder="请输入报修回复" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :disabled="disabled" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listRepairRecord,
    updateRepairRecord,
  } from "@/api/chargepile/repairRecord";
  import {
    listRepairPileNumber,
  } from "@/api/chargepile/chargePileManage";

  export default {
    name: "RepairRecordManage",
    dicts: ['charge_type', 'request_status'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 岗位表格数据
        RepairRecordList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          beginTime: null,
          endTime: null,
          deviceType: null,
          deviceNumber: null,
          onlineStatus: null
        },
        queryNumber: [],
        // 日期范围
        dateRange: [],
        dateRangeCompleted: [],
        // 表单参数
        form: {},
        // 表单禁用
        disabled: false,
        // 表单校验
        rules: {
          requestStatus: [
            { required: true, message: "报修状态不能为空", trigger: "blur" }
          ],
          adminResponse: [
            { required: true, message: "报修回复不能为空", trigger: "blur" }
          ]
        },
      };
    },
    created() {
      this.getList();
      this.listRepairPileNumber();
    },
    methods: {
      // 设备编号查询自动补全
      querySearchAsync(queryString, cb) {
        var number = this.queryNumber;
        var results = queryString ? number.filter(this.createStateFilter(queryString)) : number;

        clearTimeout(this.timeout);
        this.timeout = setTimeout(() => {
          cb(results);
        }, 3000 * Math.random());
      },
      createStateFilter(queryString) {
        return (state) => {
          return (state.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
        };
      },
      listRepairPileNumber(){
        listRepairPileNumber().then(response => {
          this.queryNumber = response.data;
          console.log(this.queryNumber)
        });
      },
      /** 查询列表 */
      getList() {
        this.loading = true;
        if(this.dateRange !== null){
          this.queryParams.beginTime = this.dateRange[0]
          this.queryParams.endTime = this.dateRange[1]
        }else{
          this.queryParams.beginTime = null;
          this.queryParams.endTime = null;
        }
        if(this.dateRangeCompleted !== null){
          this.queryParams.beginTimeCompleted = this.dateRangeCompleted[0]
          this.queryParams.endTimeCompleted = this.dateRangeCompleted[1]
        }else{
          this.queryParams.beginTimeCompleted = null;
          this.queryParams.endTimeCompleted = null;
        }
        listRepairRecord(this.queryParams).then(response => {
          this.RepairRecordList = response.data.list;
          this.total = response.data.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
        this.disabled = false;
      },
      // 表单重置
      reset() {
        this.form = {
          repairRecordId: undefined,
          chargePileId: undefined,
          requestStatus: undefined,
          adminResponse: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange = []
        this.dateRangeCompleted = []
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.postId)
        this.single = selection.length!=1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.disabled = false;
        this.reset();
        this.open = true;
        this.title = "添加设备";
        this.initAMap()
      },
      /** 查看按钮操作 */
      handleInfo(row){
        this.handleUpdate(row)
        this.title = "查看设备";
        this.disabled = true;
        // this.initAMap()
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.disabled = false;
        this.reset();
        this.form.repairRecordId = row.id;
        this.form.chargePileId = row.chargePileId;
        this.open = true;
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            console.log(this.form)
            updateRepairRecord(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        });
      },
    }
  };
</script>
